package Panel;

import Bean.Department;
import Dao.DepartDao;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class DepartmentManagementPanel extends JPanel {
    private JTable table;
    private JScrollPane jsp;

    public DepartmentManagementPanel() {
        this.setLayout(null);
        this.setBounds(0, 100, 680, 365);
        this.setBackground(new Color(66, 203, 222));
        JLabel where = new JLabel("查询条件:");
        JComboBox whereCb = new JComboBox(new String[]{"编号", "部门名称", "负责人", "描述"});
        JTextField whereI = new JTextField();
        JButton query = new JButton("搜索");
        //获取数据
        table = new JTable(getTableList(DepartDao.getDepartmentList()), new String[]{"编号", "部门名称", "负责人", "描述"});
        jsp = new JScrollPane(table);
        where.setBounds(90, 0, 100, 30);
        whereCb.setBounds(150, 5, 100, 20);
        whereI.setBounds(300, 5, 150, 20);
        query.setBounds(480, 5, 80, 20);
        table.setBounds(0, 0, 600, 200);
        jsp = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        jsp.setBounds(40, 80, 600, 200);

        //搜索实现
        query.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String item = (String) whereCb.getSelectedItem();
                System.out.println(item);
                String text = whereI.getText();
                System.out.println(text);
                List<Department> query1 = DepartDao.query(item, text);
                reDraw(query1);
            }
        });
        this.add(where);
        this.add(whereCb);
        this.add(whereI);
        this.add(query);
        this.add(jsp);

        JButton add = new JButton("添加");
        JButton mod = new JButton("修改");
        JButton del = new JButton("删除");
        add.setBounds(150, 330, 80, 20);
        mod.setBounds(300, 330, 80, 20);
        del.setBounds(450, 330, 80, 20);
        this.add(add);
        this.add(mod);
        this.add(del);

        //事件
        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                showModelPlane("添加部门信息", "添加");
            }
        });

        mod.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                showModelPlane("修改部门信息","修改");
            }
        });

        del.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                JFrame j = new JFrame("删除部门信息");
                j.setLayout(null);
                j.setBounds(450, 300, 600, 300);
                JLabel id = new JLabel("编号");
                JTextField idI = new JTextField();
                JButton d = new JButton("删除");
                JButton c = new JButton("关闭");
                id.setBounds(0, 30, 80, 20);
                idI.setBounds(110, 30, 120, 20);
                d.setBounds(150, 180, 80, 20);
                c.setBounds(300, 180, 80, 20);
                j.add(id);
                j.add(idI);
                j.add(d);
                j.add(c);

                d.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent actionEvent) {
                        List<Integer> id1 = DepartDao.findId();
                        Boolean b = null;
                       try {
                           if(!id1.contains(Integer.parseInt(idI.getText()))){
                               JOptionPane.showMessageDialog(j,"请输入已存在的id");
                               return;
                           }else {
                               b = DepartDao.delInfo(Integer.parseInt(idI.getText()));
                           }
                       }catch (NumberFormatException e){
                           JOptionPane.showMessageDialog(j,"请输入正确类型的数据");
                       }
                        if(b){
                            JOptionPane.showMessageDialog(j,"删除成功");
                            reDraw(DepartDao.getDepartmentList());
                        }else{
                            JOptionPane.showMessageDialog(j,"删除失败 请重试");

                        }


                    }
                });
                c.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent actionEvent) {
                        j.dispose();
                    }
                });
                j.setVisible(true);
            }
        });
        this.setVisible(true);
    }

    public String[][] getTableList(List<Department> l) {
        List<Department> list = l;
        String[][] tableList = new String[list.size()][4];
        for (int i = 0; i < list.size(); i++) {
            tableList[i][0] = String.valueOf(list.get(i).getId());
            tableList[i][1] = list.get(i).getName();
            tableList[i][2] = list.get(i).getCharging();
            tableList[i][3] = list.get(i).getDescribe();
        }
        return tableList;
    }

    public void reDraw(List<Department> list) {
        this.setVisible(false);
        this.remove(jsp);
        table = new JTable(getTableList(list), new String[]{"编号", "部门名称", "负责人", "描述"});
        jsp.setViewportView(table);
        this.add(jsp);
        this.setVisible(true);
    }

    public void showModelPlane(String tit, String buttonName) {
        JFrame frame = new JFrame(tit);
        frame.setLayout(null);
        frame.setBounds(450, 300, 600, 300);
        JLabel id = new JLabel("编号:");
        JLabel name = new JLabel("部门名称:");
        JLabel charging = new JLabel("负责人:");
        JLabel des = new JLabel("描述:");
        id.setBounds(0, 30, 80, 20);
        name.setBounds(300, 30, 80, 20);
        charging.setBounds(0, 80, 80, 20);
        des.setBounds(300, 80, 80, 20);
        frame.add(id);
        frame.add(name);
        frame.add(charging);
        frame.add(des);

        JTextField idI = new JTextField();
        JTextField nameI = new JTextField();
        JTextField chargingI = new JTextField();
        JTextField desI = new JTextField();
        idI.setBounds(110, 30, 120, 20);
        nameI.setBounds(350, 30, 120, 20);
        chargingI.setBounds(110, 80, 120, 20);
        desI.setBounds(350, 80, 120, 20);
        frame.add(idI);
        frame.add(nameI);
        frame.add(chargingI);
        frame.add(desI);

        JButton add = new JButton(buttonName);
        JButton close = new JButton("关闭");
        add.setBounds(150, 180, 80, 20);
        close.setBounds(300, 180, 80, 20);
        frame.add(add);
        frame.add(close);

        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                Department d = new Department();
                List<Integer> id1 = DepartDao.findId();
                Boolean b= null;
                System.out.println(id1.contains(Integer.parseInt(idI.getText())));
                try {
                    if (!id1.contains(Integer.parseInt(idI.getText())) && "添加".equals(buttonName)) {
                        d.setId(Integer.parseInt(idI.getText()));
                    } else if(id1.contains(Integer.parseInt(idI.getText())) && "添加".equals(buttonName)){
                        JOptionPane.showMessageDialog(frame, "请输入不存在的id");
                        return;
                    }
                    else if(id1.contains(Integer.parseInt(idI.getText())) && "修改".equals(buttonName)){
                        d.setId(Integer.parseInt(idI.getText()));
                    }else if(!id1.contains(Integer.parseInt(idI.getText())) && "修改".equals(buttonName)){
                        JOptionPane.showMessageDialog(frame, "请输入存在的id");
                    System.out.println(d.getId());
                        return;
                    }
                } catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(frame, "请输入正确类型的数据");
                    return;
                }
                d.setName(nameI.getText());
                d.setCharging(chargingI.getText());
                d.setDescribe(desI.getText());
                if("添加".equals(buttonName)){
                b =DepartDao.addInfo(d);

                }else if("修改".equals(buttonName)){
                    b = DepartDao.modInfo(d);
                }
                if (b){
                    JOptionPane.showMessageDialog(frame,buttonName+"成功");
                    reDraw(DepartDao.getDepartmentList());
                }else {
                    JOptionPane.showMessageDialog(frame,"添加失败 请重试");

                }
            }
        });
        close.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                frame.dispose();
            }
        });
        frame.setVisible(true);
    }
}
